<?php
$isadmin = 1;
include_once '../../comm/config.php';
include '../../comm/checkpostandget.php';
include '../check.php';

function sel_sql($table, $sel_field, $where, $alert = 0) {
	global $BIAOTOU;
	$sql = "select $sel_field from $BIAOTOU$table where $where";
	if ($alert == 1) {
		echo $sql;
	}
	$query = mysql_query($sql);
	if ($query) {
		while ($row = mysql_fetch_array($query)) {
			if (strpos($sel_field, ",") or $sel_field == "*") {
				$arr = $row;
			} else {
				$arr = $row[$sel_field];
			}
		}
	}
	return $arr;
}

function update_sql($table, $field_arr, $where, $xs, $alert = 0) { //$xs 0表示更新 1表示同加  2表示同减,$alert默认为0,1表示打印sql语句,2表示先执行在打印
	global $BIAOTOU;
	$sql = "";
	foreach ($field_arr as $k => $v) {
		if ($xs == 0) {
			$sql = "`$k`='$v'," . $sql;
		}
		elseif ($xs == 1) {
			$sql = "`$k`=`$k`+'$v'," . $sql;
		}
		elseif ($xs == 2) {
			$sql = "`$k`=`$k`-'$v'," . $sql;
		}
	}
	$sql = substr($sql, 0, strlen($sql) - 1);
	$sql = "update $BIAOTOU$table set $sql where $where;";
	if ($alert == 0) {
		mysql_query($sql);
	}
	elseif ($alert == 1) {
		echo $sql;
	}
	elseif ($alert == 2) {
		mysql_query($sql);
		echo $sql;
	}
}

function insert_one_sql($table, $field_arr, $alert = 0) {
	global $BIAOTOU;
	$field = "";
	$values = "";
	foreach ($field_arr as $k => $v) {
		$field = "`" . $k . "`," . $field;
		$values = "'" . $v . "'," . $values;
	}
	$field = substr($field, 0, strlen($field) - 1);
	$values = substr($values, 0, strlen($values) - 1);
	$sql = "insert into $BIAOTOU$table($field) values ($values);";
	$query = mysql_query($sql);
	if ($alert == 0) {
		if ($query)
			return mysql_insert_id();
		else
			return mysql_error();
	}
	elseif ($alert == 1) {
		echo $sql;
	}
	elseif ($alert == 2) {
		echo $sql;
		if ($query)
			return 1;
		else
			return mysql_error();
	}
}

function inc_fanli($ddusername, $fxje, $tgje, $order_code,$merchant_id) {
	$field_arr=array('money'=>$fxje,'dengji'=>1);
	update_sql('user', $field_arr, "ddusername='$ddusername'", 1); //增加会员金额和等级

	//用户消息
	$title = '您获得了新的商城交易返现！';
	$trade_id = $order_code;
	$msg_tabao = "您获得了新的交易返现，".$merchant_id."商城订单号" . $order_code . "返现金额" . $fxje;
	$filed_arr = array (
		'title' => $title,
		'content' => $msg_tabao,
		'addtime' => date('Y-m-d H:i:s'
	), 'see' => 0, 'ddusername' => $ddusername, 'senduser' => '网站客服');
	insert_one_sql("msg", $filed_arr);

	//用户明细
	$shijian = "商城交易返现";
	$memo = $merchant_id."交易号$order_code";
	$filed_arr = array (
		'ddusername' => $ddusername,
		'shijian' => $shijian,
		'addtime' => date('Y-m-d H:i:s'
	), 'je' => $fxje, 'memo' => $memo);
	insert_one_sql("mingxi", $filed_arr);

	//求推荐人
	$tjrid = sel_sql("user", "tjr", "ddusername='$ddusername'");
	if ($tjrid > 0) {
		$tjrname = sel_sql("user", "ddusername", "Id='$tjrid'");
		//增加推荐人佣金
		$field_arr = array (
			'money' => $tgje
		);
		update_sql("user", $field_arr, "Id='$tjrid'", 1);

		//用户消息
		$title = '您获得了新的推广佣金！';
		$msg_taobaotuiguang = "您获得了新的推广佣金" . $tgje;
		$filed_arr = array (
			'title' => $title,
			'content' => $msg_taobaotuiguang,
			'addtime' => date('Y-m-d H:i:s'
		), 'see' => 0, 'ddusername' => $tjrname, 'senduser' => '网站客服');
		insert_one_sql("msg", $filed_arr);

		//用户明细
		$shijian = "推广佣金";
		$memo = "交易人$ddusername";
		$filed_arr = array (
			'ddusername' => $tjrname,
			'shijian' => $shijian,
			'addtime' => date('Y-m-d H:i:s'
		), 'je' => $tgje, 'memo' => $memo);
		insert_one_sql("mingxi", $filed_arr);
	}
}

function get_object_vars_final($obj) {
	if (is_object($obj)) {
		$obj = get_object_vars($obj);
	}

	if (is_array($obj)) {
		$n = 0;
		foreach ($obj as $key => $value) {
			if (($key == 'bill_yyyymmdd' or $key == 'u_id') and $n > 0) {
				if (is_object($value))
					$value = '';
			}
			$obj[$key] = get_object_vars_final($value);
			$n++;
		}
	}
	return $obj;
}

$etime = date("Y-m-d");
$stime = date('Y-m-d', strtotime("-30 day"));

if ($_POST['daoru'] != NULL) {
	$mm = md5("linktech^" . MALLMM);
	$syyyymmdd = date('Ymd', strtotime(trim($_POST['syyyymmdd'])));
	$eyyyymmdd = date('Ymd', strtotime(trim($_POST['eyyyymmdd'])));

	$linktech = "http://www.linktech.cn/AC/trans_list.htm?account_id=" . MALLZH . "&sign=" . $mm . "&syyyymmdd=" . $syyyymmdd . "&eyyyymmdd=" . $eyyyymmdd . "&affiliate_id=" . MALLWZBH . "&output_type=xml&type=cps";
	$content = file_get_contents($linktech);
	$xmlCode = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
	$arrdata = get_object_vars_final($xmlCode);
	$result = $arrdata['is_success']['@attributes']['result'];
	$count = $arrdata['order_list']['@attributes']['count'];
	$arr_data = $arrdata['order_list']['order'];
	
	if ($count == 1) {
		$arr_data = array (0 => $arr_data);
	}

	$i = 0; //记录新订单
	$j = 0; //记录更新订单
	foreach ($arr_data as $order) {
		if ($count > 0) {
			$order_time = $order['order_time']; //下单时间
			$merchant_id = $order['merchant_id']; //商城联盟ID
			$u_id = $order['u_id']; //会员ID
			if ($u_id != '') {
				$dduser = sel_sql("user", "ddusername,dengji", "Id='$u_id'");
				$ddusername = $dduser['ddusername'];
				$dengji = $dduser['dengji'];
			} else {
				$u_id = 0;
				$ddusername = '';
				$dengji = 0;
			}

			$order_code = $order['order_code']; //订货号
			$product_code = $order['product_code']; //商品号，在购物车中，商品的订货号相同，用商品号区别
			$item_price = $order['item_price']; //单价
			$item_count = $order['item_count']; //数量
			$sales = $order['sales']; //总额
			$commission = $order['commission']; //返利金额
			$stat_desc = $order['stat_desc']; //订单状态
			$bill_yyyymmdd = $order['bill_yyyymmdd']; //结算日
			$fxje = mall_ck_fxje($commission, $dengji); //会员返利金额
			$tgje = round($commission * TGBL, 2); //给会员的推广佣金
			//添加交易记录
			$id = sel_sql("mall_order", "id", "order_code='$order_code' and product_code='" . $product_code . "'"); //用订单号验证该交易是否存在于数据库
			if ($id == '') { //交易不存在
				$filed_arr = array (
					'order_time' => $order_time,
					'product_code' => $product_code,
					'merchant_id' => $merchant_id,
					'u_id' => $u_id,
					'order_code' => $order_code,
					'item_count' => $item_count,
					'item_price' => $item_price,
					'sales' => $sales,
					'commission' => $commission,
					'stat_desc' => $stat_desc,
					'bill_yyyymmdd' => $bill_yyyymmdd,
					'fxje' => $fxje,
					'ddusername' => $ddusername,
					'lm' => 'linktech'
				);
				$id=insert_one_sql("mall_order", $filed_arr);
				$i = $i +1;
				if ($stat_desc == "核对有效" and $u_id != '') {
					$field_arr_order = array (
						'stat_desc' => '核对有效',
						'qrsj'=>date('Y-m-d H:i:s')
					);
					$order_code=$order_code.'|'.$product_code;
					update_sql('mall_order', $field_arr_order, "id='$id'", 0);
					inc_fanli($ddusername, $fxje, $tgje, $order_code, $merchant_id);
				}
			} else {
				$table = sel_sql("mall_order", "stat_desc,u_id,ddusername", "id='$id'"); //订单存在，求此订单的状态
				$table_stat_desc=$table['stat_desc'];
				$ddusername=$ddusername?$ddusername:$table['ddusername'];
				$u_id=$u_id?$u_id:$table['u_id'];
				if ($table_stat_desc != $stat_desc) {
					$filed_arr = array (
						'stat_desc' => $stat_desc,
						'bill_yyyymmdd' => $bill_yyyymmdd
					);
					update_sql("mall_order", $filed_arr, "id='$id'"); //更新订单状态
					if ($u_id != '' and $stat_desc == '核对有效') {
						$field_arr_order = array (
							'stat_desc' => '核对有效',
						    'qrsj'=>date('Y-m-d H:i:s')
						);
						update_sql('mall_order', $field_arr_order, "id='$id'", 0);
						$order_code=$order_code.'|'.$product_code;
						inc_fanli($ddusername, $fxje, $tgje, $order_code, $merchant_id);
						++ $j;
					}
				}
			}
		}
	}

	$sql = "delete from " . $BIAOTOU . "mall_order where lm='linktech' and product_code is NULL";
	mysql_query($sql);

	if ($i != 0)
		$msgi = "成功导入订单" . $i . "条";
	if ($j != 0)
		$msgj = "成功更新订单" . $j . "条";
	if ($i == 0 and $j == 0) {
		$k = 1;
		$msg = "已入库，无需操作";
	}
}
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理中心</title>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.STYLE1 {color: #FF3300}
.STYLE2 {color: #009900}
-->
</style>
</head>

<body topmargin="5" leftmargin="0">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="282" valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>
          <table cellspacing="0" width="100%" height="207" style="border:1px  solid #DCEAF7;">
            <tr>
              <td height="30" align="left" bgcolor="E9F2FB" class="bigtext"><strong>&nbsp;<img src="../images/arrow.gif" width="16" height="22" align="absmiddle" /> 获取交商城订单</strong></td>
              </tr>
            <tr>
              <td valign="top"><form method="post" name="form1" action="">
              <input type="hidden" name="action" value="getorders"  />
              <input type="hidden" value="1"  />
<br />
<br />
<table width="589" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="40" colspan="2" class="bigtext"><span class="STYLE2">
      注意时间格式！！！
      </span></td>
    </tr>
    <tr>
    <td width="121" height="40" class="bigtext">时间范围：</td>
    <td width="468" height="40" class="bigtext">
      <input name="syyyymmdd" type="text" size="10" value="<?php echo $stime ?>" />
     到  
     <input name="eyyyymmdd" type="text" size="10" value="<?php echo $etime ?>" />    </td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
    <td height="40"><input type="submit" name="daoru" value="获取交易记录" size="17"></td>
  </tr>
  <tr>
    <td height="40" colspan="2" class="large-green"><?php if($i!=0 or $j!=0 or $k!=0){echo $_POST['syyyymmdd']."至".$_POST['eyyyymmdd']."，共有订单".$count."条<br/>"; }?><?php if($i!=0) echo $msgi."<br/>"; if($j!=0) echo $msgj; if($k==1) echo $msg; ?>
      </td>
    </tr>
</table>

</form>

</td>
                </tr>
              </table></td>
            </tr>
          </table></td>
        </tr>
      
    </table></td>
  </tr>
</table>
</body>
</html>